import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: const Center(
          child: Text("自定义滑动组件"),
        )),
        body: MyHomeContent());
  }
}

class Mem extends StatelessWidget {
  final String content;

  Mem(this.content);

  @override
  Widget build(BuildContext context) {
    return Title(
        color: Colors.black,
        child: Text(content, style: TextStyle(fontSize: 16)));
  }
}

class MyHomeContent extends StatefulWidget {
  @override
  State<MyHomeContent> createState() => _MyHomeContentState();
}

class _MyHomeContentState extends State<MyHomeContent> {
  @override
  Widget build(BuildContext context) {
    var divider = Column(
      children: [
        const Divider(height: 1, color: Colors.black),
        SizedBox.fromSize(size: Size.fromHeight(10))
      ],
    );

    return ListView(children: [
      Mem("SliverGrid 自定义"),
      Container(
        height: 200,
        child: CustomScrollView(slivers: [
          SliverSafeArea(
              sliver: SliverPadding(
                  padding: EdgeInsets.all(8),
                  sliver: SliverGrid(
                    delegate: SliverChildBuilderDelegate(
                        (context, index) => Container(
                                color: Color.fromARGB(
                              255,
                              Random().nextInt(256),
                              Random().nextInt(256),
                              Random().nextInt(256),
                            )),
                        childCount: 50),
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 6, childAspectRatio: 1.5),
                  )))
        ]),
      ),
      divider,
      Mem("不同的组件出现在同一个滚动视图中"),
      Container(
        height: 400,
        child: CustomScrollView(slivers: [
          SliverAppBar(
            // title: Text("Sliver 的导航"),
            expandedHeight: 150,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              title: Container(
                height: 100,
                width: 200,
                child: Stack(
                  children: [
                    Positioned(child: Text("这是 FlexibleSpaceBar"), left: -30),
                    Image.asset(
                      "assets/images/mao.jpg",
                      height: 50,
                    )
                  ],
                  alignment: AlignmentDirectional.bottomEnd,
                  clipBehavior: Clip.none,
                ),
              ),
            ),
          ),
          SliverSafeArea(
              sliver: SliverPadding(
                  padding: EdgeInsets.all(8),
                  sliver: SliverList(
                    delegate: SliverChildBuilderDelegate(
                        (context, index) => ListTile(
                              leading: Icon(Icons.person),
                              title: Text("联系人 $index"),
                              subtitle: Text("123435465"),
                            ),
                        childCount: 5),
                  ))),
          SliverGrid(
            delegate: SliverChildBuilderDelegate(
                (context, index) => Container(
                        color: Color.fromARGB(
                      255,
                      Random().nextInt(256),
                      Random().nextInt(256),
                      Random().nextInt(256),
                    )),
                childCount: 20),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 6, childAspectRatio: 1.5),
          )
        ]),
      ),
      divider,
    ]);
  }
}
